package com.lottery.ruok.lottrey.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.adapter.MyFragmentStatePagerAdapter;
import com.lottery.ruok.lottrey.fragment.AptitudeFragment;
import com.lottery.ruok.lottrey.fragment.BaseFragment;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.GetCurrentIssueNoRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.GetCurrentIssueNoResponse;
import com.lottery.ruok.lottrey.ui.ShapeIndicatorView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import utils.Utils;
/*
* 智能追号
* */

public class AptitudeLotteryActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @BindView(R.id.time)
    TextView time;

    @BindView(R.id.viewPager)
    ViewPager viewPager;

    CountDownTimer countDownTimer;

    AptitudeFragment aptitudeFragment1;
    AptitudeFragment aptitudeFragment2;
    @BindView(R.id.slide)
    public ImageView slide;
    @BindView(R.id.result)
    public TextView result;
    public boolean is_win_stop = false;
    public int nowFocus = 0;//当前的焦点在哪一个追号界面
    @BindView(R.id.custom_indicator)
    ShapeIndicatorView shapeIndicatorView;
    @BindView(R.id.tabs)
    TabLayout mTabLayout;

    private void setTimer() {
        RetrofitBase.getRetrofit().create(GetCurrentIssueNoRequest.class).getResult(getIntent().getStringExtra("code"))
                .compose(RxHelper.<GetCurrentIssueNoResponse>handleResultReConnection())
                .subscribe(new RxSubscribe<GetCurrentIssueNoResponse>(this) {
                    @Override
                    protected void _onNext(final GetCurrentIssueNoResponse getCurrentIssueNoResponse) {
                        if (aptitudeFragment1 != null) {
                            aptitudeFragment1.refreshList();
                        }
                        if (aptitudeFragment2 != null) {
                            aptitudeFragment2.refreshList();
                        }
                        if(countDownTimer != null){
                            countDownTimer.cancel();
                        }
                        //启动定时器
                        countDownTimer = new CountDownTimer(getCurrentIssueNoResponse.getTime() * 1000, 1000) {
                            @Override
                            public void onTick(long l) {
                                time.setText(Html.fromHtml("第" + getCurrentIssueNoResponse.getNo() + "期投注截止时间  " + "<font color='#3f3f3f'>" + Utils.getCountTimeByLong(l) + "</font>"));
                            }

                            @Override
                            public void onFinish() {
                                time.setText(Html.fromHtml("第" + getCurrentIssueNoResponse.getNo() + "期投注截止时间  " + "<font color='#3f3f3f'>" + "00:00:00" + "</font>"));

                                time.postDelayed(new Runnable() {
                                    @Override
                                    public void run() {
                                        setTimer();
                                    }
                                }, 2000);
                            }
                        };
                        countDownTimer.start();
                    }

                    @Override
                    protected boolean showDialog() {
                        return false;
                    }
                });
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_aptitude_lottery);

        ButterKnife.bind(this);

        init();
    }

    private void init() {
        aptitudeFragment1 = new AptitudeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("code", getIntent().getStringExtra("code"));
        bundle.putFloat("money", getIntent().getFloatExtra("money", 0));
        bundle.putLong("no", getIntent().getLongExtra("no", 0));
        bundle.putBoolean("isAdvanced", false);
        aptitudeFragment1.setArguments(bundle);

        aptitudeFragment2 = new AptitudeFragment();
        Bundle bundle2 = new Bundle();
        bundle2.putString("code", getIntent().getStringExtra("code"));
        bundle2.putFloat("money", getIntent().getFloatExtra("money", 0));
        bundle2.putLong("no", getIntent().getLongExtra("no", 0));
        bundle2.putBoolean("isAdvanced", true);
        aptitudeFragment2.setArguments(bundle2);

        slide.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (is_win_stop) {
                    is_win_stop = false;
                    slide.setImageResource(R.mipmap.check_none);

                    //sendAnalyticMsg("lottery_" + BetListActivity.ANALYTIC_VERSION + "_betrecord_autoadd_close");
                } else {
                    is_win_stop = true;
                    slide.setImageResource(R.mipmap.check_ok);

                    //sendAnalyticMsg("lottery_" + BetListActivity.ANALYTIC_VERSION + "_betrecord_autoadd_open");
                }
            }
        });

        setTimer();

        viewPager.setOffscreenPageLimit(2);

        ArrayList<BaseFragment> fragments = new ArrayList<>();
        fragments.add(aptitudeFragment1);
        fragments.add(aptitudeFragment2);
        String[] titles = new String[2];
        titles[0] = "普通追号";
        titles[1] = "高级追号";
        MyFragmentStatePagerAdapter pagerAdapter = new MyFragmentStatePagerAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setOnPageChangeListener(this);

//        result.setText(getIntent().getStringExtra("money"));
        //给Tabs设置适配器
        mTabLayout.setTabsFromPagerAdapter(pagerAdapter);
        //将TabLayout和ViewPager关联起来

        mTabLayout.setupWithViewPager(viewPager);

        shapeIndicatorView.setupWithTabLayout(mTabLayout);
        shapeIndicatorView.setupWithViewPager(viewPager);
    }

    @Override
    protected void onDestroy() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        super.onDestroy();


    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            result.setText(aptitudeFragment1.wholeNum + "期" + aptitudeFragment1.wholeData + "元");
            nowFocus = 0;
        } else {
            result.setText(aptitudeFragment2.wholeNum + "期" + aptitudeFragment2.wholeData + "元");
            nowFocus = 1;
        }
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}